package com.gcb.framework.web.service;

import com.gcb.common.core.domain.entity.SysDept;
import com.gcb.common.enums.DeptType;
import com.gcb.framework.model.LoginUser;
import com.gcb.framework.service.ISysDeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.gcb.common.core.domain.entity.SysUser;
import com.gcb.common.enums.StatusEnum;
import com.gcb.common.exception.ServiceException;
import com.gcb.common.utils.MessageUtils;
import com.gcb.common.utils.StringUtils;
import com.gcb.framework.service.ISysUserService;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 用户验证处理
 *
 * @author ruoyi
 */
@Service("userDetailsByAccount")
public class UserDetailsServiceImpl implements UserDetailsService {
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = userService.selectUserByPhone(username);
        if (StringUtils.isNull(user)) {
            log.info("登录用户：{} 不存在.", username);
            throw new ServiceException(MessageUtils.message("user.not.exists"));
        } else if (StatusEnum.DELETED.getCode().equals(user.getDelFlag())) {
            log.info("登录用户：{} 已被删除.", username);
            throw new ServiceException(MessageUtils.message("user.password.delete"));
        } else if (StatusEnum.DISABLE.getCode().equals(user.getStatus())) {
            log.info("登录用户：{} 已被停用.", username);
            throw new ServiceException(MessageUtils.message("user.blocked"));
        }

        passwordService.validate(user);

        return createLoginUser(user);
    }

    public UserDetails createLoginUser(SysUser user){
        Long operateId = null;
        //查询当前用户所属运营商id
        if (!ObjectUtils.isEmpty(user.getDeptId())) {
            SysDept sysDept = sysDeptService.selectDeptById(user.getDeptId());
            if (DeptType.OPERATE.getCode().equals(sysDept.getDeptType())) {
                operateId = user.getDeptId();
            } else {
                List<String> list = Arrays.asList(sysDept.getAncestors().split(","));
                List<SysDept> sysDepts = sysDeptService.selectDeptByIds(list);
                for (SysDept dept : sysDepts) {
                    if (DeptType.OPERATE.getCode().equals(dept.getDeptType())) {
                        operateId = dept.getDeptId();
                        break;
                    }
                }

            }
        }
        return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user), operateId);
    }
}
